﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eloquera.Client;
using BlogEngine.Core;
using System.Web.Security;
using BlogEngine.Core.Providers;
using BlogEngine.Core.Providers.EloqueraProvider.Db;
using BlogEngine.Core.DataStore;
using System.Security.Cryptography;

namespace EloqueraInstall {
    class EqInstall {
        static void Main(string[] args) {
            DB eqDb;
            string conStr = System.Web.Configuration.WebConfigurationManager.AppSettings["Eloquera_Con_String"];
            if (String.IsNullOrEmpty(conStr)) {
                conStr = "server=localhost;password=;options=none;";
            }
            eqDb = new DB(conStr);
            eqDb.CreateDatabase("BlogNetForum");
            eqDb.OpenDatabase("BlogNetForum");

            //-------------------Registering Types---------
            eqDb.RegisterType(typeof (EqCategory));
            eqDb.RegisterType(typeof (EqDataStore));
            eqDb.RegisterType(typeof (EqMembershipUser));
            eqDb.RegisterType(typeof (EqPage));
            eqDb.RegisterType(typeof(EqPingServices));
            eqDb.RegisterType(typeof (EqPost));
            eqDb.RegisterType(typeof(EqRole));
            eqDb.RegisterType(typeof (EqProfile));
            eqDb.RegisterType(typeof (EqRoleProvider));
            eqDb.RegisterType(typeof(EqSettings));
            eqDb.RegisterType(typeof(EqStopWords));
            //---------------------------------------------

            string[] settings = new string[] {"administratorrole", "Administrators",
"alternatefeedurl", "",
"authorname", "My name",
"avatar", "combine",
"blogrollmaxlength", "23",
"blogrollupdateminutes", "60",
"blogrollvisibleposts", "3",
"contactformmessage", "<p>I will answer the mail as soon as I can.</p>",
"contactthankmessage", "<h1>Thank you</h1><p>The message was sent.</p>",
"culture", "Auto",
"dayscommentsareenabled", "0",
"description", "Short description of the blog",
"displaycommentsonrecentposts", "True",
"displayratingsonrecentposts", "True",
"email", "user@example.com",
"emailsubjectprefix", "Weblog",
"enablecommentsearch", "True",
"enablecommentsmoderation", "False",
"enablecontactattachments", "True",
"enablecountryincomments", "True",
"enablehttpcompression", "True",
"enableopensearch", "True",
"enablepingbackreceive", "True",
"enablepingbacksend", "True",
"enablerating", "True",
"enablereferrertracking", "False",
"enablerelatedposts", "True",
"enablessl", "False",
"enabletrackbackreceive", "True",
"enabletrackbacksend", "True",
"endorsement", "http://www.dotnetblogengine.net/syndication.axd",
"fileextension", ".aspx",
"geocodinglatitude", "0",
"geocodinglongitude", "0",
"handlewwwsubdomain", "",
"iscocommentenabled", "False",
"iscommentsenabled", "True",
"language", "en-GB",
"mobiletheme", "Mobile",
"name", "Name of the blog",
"numberofrecentcomments", "10",
"numberofrecentposts", "10",
"postsperfeed", "10",
"postsperpage", "10",
"removewhitespaceinstylesheets", "True",
"searchbuttontext", "Search",
"searchcommentlabeltext", "Include comments in search",
"searchdefaulttext", "Enter search term",
"sendmailoncomment", "True",
"showdescriptioninpostlist", "False",
"showlivepreview", "True",
"showpostnavigation", "True",
"smtppassword", "password",
"smtpserver", "mail.example.dk",
"smtpserverport", "25",
"smtpusername", "user@example.com",
"storagelocation", "~/App_Data/",
"syndicationformat", "Rss",
"theme", "Standard",
"timestamppostlinks", "True",
"timezone", "-5",
"trackingscript", ""};
            EqSettings set = new EqSettings();
            List<string> sKey = new List<string>();
            List<string> sVal = new List<string>();

            for (int i = 0; i < settings.Length; i += 2) {
                sKey.Add(settings[i]);
                sVal.Add(settings[i+1]);
            }
            set.Keys = sKey.ToArray();
            set.Values = sVal.ToArray();
            eqDb.Store(set);


            string[] pings = new string[] { "http://rpc.technorati.com/rpc/ping",
"http://rpc.pingomatic.com/rpc2",
"http://ping.feedburner.com",
"http://www.bloglines.com/ping",
"http://services.newsgator.com/ngws/xmlrpcping.aspx",
"http://api.my.yahoo.com/rpc2 ",
"http://blogsearch.google.com/ping/RPC2",
"http://rpc.pingthesemanticweb.com/"};
            EqPingServices ping = new EqPingServices();
            ping.Services = pings;
            eqDb.Store(ping);


            string[] stWords = new string[] {"a",
"about",
"actually",
"add",
"after",
"all",
"almost",
"along",
"also",
"an",
"and",
"any",
"are",
"as",
"at",
"be",
"both",
"but",
"by",
"can",
"cannot",
"com",
"could",
"de",
"do",
"down",
"each",
"either",
"en",
"for",
"from",
"good",
"has",
"have",
"he",
"her",
"here",
"hers",
"his",
"how",
"i",
"if",
"in",
"into",
"is",
"it",
"its",
"just",
"la",
"like",
"long",
"make",
"me",
"more",
"much",
"my",
"need",
"new",
"now",
"of",
"off",
"on",
"once",
"one",
"ones",
"only",
"or",
"our",
"out",
"over",
"own",
"really",
"right",
"same",
"see",
"she",
"so",
"some",
"such",
"take",
"takes",
"that",
"the",
"their",
"these",
"thing",
"this",
"to",
"too",
"took",
"und",
"up",
"use",
"used",
"using",
"very",
"was",
"we",
"well",
"what",
"when",
"where",
"who",
"will",
"with",
"www",
"you",
"your"};
            EqStopWords stopW = new EqStopWords();
            stopW.Words = stWords;
            eqDb.Store(stopW);

            EqCategory cat = new EqCategory() {
                Id = new Guid("ffc26b8b-7d45-46e3-b702-7198e8847e06"),
                Parent = new Guid("daf4bc0e-f4b7-4895-94b2-3b1413379d4b")
            };
            eqDb.Store(cat);

            EqPost post = new EqPost() {
                Id = new Guid("daf4bc0e-f4b7-4895-94b2-3b1413379d4b"),
                Title = "Welcome to BlogEngine.NET 1.5 using Eloquera",
                Description = "The description is used as the meta description as well as shown in the related posts. It is recommended that you write a description, but not mandatory",
                Content = "<p>If you see this post it means that BlogEngine.NET 1.5 is running with Eloquera and the Eloquera is configured correctly.</p>"+
	                        "<h2>Setup</h2>"+
	                        "<p>You are configured to have your user names and passwords stored in Eloquera by default.  It is time to setup some users. Find the sign-in link located either at the bottom or top of the page depending on your current theme and click it. Now enter \"admin\" in both the username and password fields and click the button. You will now see an admin menu appear. It has a link to the \"Users\" admin page.  You should also see a link to a Change Password page.  You should use this to change the default password right away.</p>"+
	                        "<h2>Write permissions</h2>"+
	                        "<p>Since you are using Eloquera to store your posts, most information is stored there.  However, if you want to store attachments or images in the blog, you will want write permissions setup on the App_Data folder.</p>"+
	                        "<h2>On the web </h2>"+
	                        "<p>You can find BlogEngine.NET on the <a href=\"http://www.dotnetblogengine.net\">official website</a>. Here you will find tutorials, documentation, tips and tricks and much more. The ongoing development of BlogEngine.NET can be followed at <a href=\"http://www.codeplex.com/blogengine\">CodePlex</a> where the daily builds will be published for anyone to download.</p>"+
	                        "<p>Good luck and happy writing.</p>"+
	                        "<p>The BlogEngine.NET team</p>",
                Categories = new string[] { "daf4bc0e-f4b7-4895-94b2-3b1413379d4b" },
                DateCreated = DateTime.Now,
                DateModified = DateTime.Now,
                Author = "admin",
                IsPublished = true
            };
            eqDb.Store(post);

            EqDataStore be = new EqDataStore() {
                ExType = 1,
                ExId = "be_WIDGET_ZONE",
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-16\"?><WidgetData xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Settings>&lt;widgets&gt;&lt;widget id=\"d9ada63d-3462-4c72-908e-9d35f0acce40\" title=\"TextBox\" showTitle=\"True\"&gt;TextBox&lt;/widget&gt;&lt;widget id=\"19baa5f6-49d4-4828-8f7f-018535c35f94\" title=\"Administration\" showTitle=\"True\"&gt;Administration&lt;/widget&gt;&lt;widget id=\"d81c5ae3-e57e-4374-a539-5cdee45e639f\" title=\"Search\" showTitle=\"True\"&gt;Search&lt;/widget&gt;&lt;widget id=\"77142800-6dff-4016-99ca-69b5c5ebac93\" title=\"Tag cloud\" showTitle=\"True\"&gt;Tag cloud&lt;/widget&gt;&lt;widget id=\"4ce68ae7-c0c8-4bf8-b50f-a67b582b0d2e\" title=\"RecentPosts\" showTitle=\"True\"&gt;RecentPosts&lt;/widget&gt;&lt;/widgets&gt;</Settings></WidgetData>"
            };
            eqDb.Store(be);

            byte[] data = Encoding.UTF8.GetBytes("1");
            string pass;
            using (HashAlgorithm sha = new SHA256Managed()) {
                byte[] encryptedBytes = sha.TransformFinalBlock(data, 0, data.Length);
                pass = Convert.ToBase64String(sha.Hash);
            }

            EqMembershipUser user = new EqMembershipUser() {
                Email = "email@example.com",
                LastLoginTime = DateTime.Now,
                UserName = "Admin",
                Password = pass
            };
            eqDb.Store(user);

            EqRole role = new EqRole() {
                Name = "Administrators",
                Users = new string[] {"Admin"}
            };
            eqDb.Store(role);

            role = new EqRole() {
                Name = "Editors",
                Users = new string[]{} 
            };
            eqDb.Store(role);

            EqDataStore eds = new EqDataStore() {
                ExId = "Smilies",
                ExType = (int)ExtensionType.Extension,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ManagedExtension xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Name=\"Smilies\"><Version>1.3</Version><Description>Converts ASCII smilies into real smilies in the comments</Description><Author>BlogEngine.NET</Author><AdminPage /><Enabled>true</Enabled><ShowSettings>true</ShowSettings></ManagedExtension>"
            };
            eqDb.Store(eds);
            eds = new EqDataStore() {
                ExId = "SendPings",
                ExType = (int)ExtensionType.Extension,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ManagedExtension xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Name=\"SendPings\"><Version>1.3</Version><Description>Pings all the ping services specified on the PingServices admin page and send track- and pingbacks</Description><Author>BlogEngine.NET</Author><AdminPage /><Enabled>true</Enabled><ShowSettings>true</ShowSettings></ManagedExtension>"
            };
            eqDb.Store(eds);
            eds = new EqDataStore() {
                ExId = "SendCommentMail",
                ExType = (int)ExtensionType.Extension,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ManagedExtension xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Name=\"SendCommentMail\"><Version>1.3</Version><Description>Sends an e-mail to the blog owner whenever a comment is added</Description><Author>BlogEngine.NET</Author><AdminPage /><Enabled>true</Enabled><ShowSettings>true</ShowSettings></ManagedExtension>"
            };
            eqDb.Store(eds);
            eds = new EqDataStore() {
                ExId = "ResolveLinks",
                ExType = (int)ExtensionType.Extension,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ManagedExtension xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Name=\"ResolveLinks\"><Version>1.3</Version><Description>Auto resolves URLs in the comments and turn them into valid hyperlinks.</Description><Author>BlogEngine.NET</Author><AdminPage /><Enabled>true</Enabled><ShowSettings>true</ShowSettings></ManagedExtension>"
            };
            eqDb.Store(eds);
            eds = new EqDataStore() {
                ExId = "CodeFormatterExtension",
                ExType = (int)ExtensionType.Extension,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ManagedExtension xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Name=\"CodeFormatterExtension\"><Version>0.1</Version><Description>Converts text to formatted syntax highlighted code (beta).</Description><Author>www.manoli.net</Author><AdminPage /><Enabled>true</Enabled><ShowSettings>true</ShowSettings></ManagedExtension>"
            };
            eqDb.Store(eds);
            eds = new EqDataStore() {
                ExId = "BreakPost",
                ExType = (int)ExtensionType.Extension,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ManagedExtension xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Name=\"BreakPost\"><Version>1.2</Version><Description>Breaks a post where [more] is found in the body and adds a link to full post</Description><Author>BlogEngine.NET</Author><AdminPage /><Enabled>true</Enabled><ShowSettings>true</ShowSettings></ManagedExtension>"
            };
            eqDb.Store(eds);
            eds = new EqDataStore() {
                ExId = "BBCode",
                ExType = (int)ExtensionType.Extension,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ManagedExtension xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Name=\"BBCode\"><Version>1.0</Version><Description>Converts BBCode to XHTML in the comments</Description><Author>&lt;a href=\"http://dotnetblogengine.net\"&gt;BlogEngine.NET&lt;/a&gt;</Author><AdminPage /><Enabled>true</Enabled><Settings><Index>0</Index><Name>BBCode</Name><Help>Converts BBCode to XHTML in the comments. Close tag is optional.</Help><Delimiter>44</Delimiter><Parameters><Name>Code</Name><Label>Code</Label><MaxLength>20</MaxLength><Required>true</Required><KeyField>false</KeyField><Values>b</Values><Values>i</Values><Values>u</Values><Values>quote</Values><ParamType>String</ParamType><SelectedValue /></Parameters><Parameters><Name>OpenTag</Name><Label>Open Tag</Label><MaxLength>150</MaxLength><Required>true</Required><KeyField>false</KeyField><Values>strong</Values><Values>em</Values><Values>span style=\"text-decoration:underline\"</Values><Values>cite title=\"Quote\"</Values><ParamType>String</ParamType><SelectedValue /></Parameters><Parameters><Name>CloseTag</Name><Label>Close Tag</Label><MaxLength>100</MaxLength><Required>false</Required><KeyField>false</KeyField><Values /><Values /><Values>span</Values><Values>cite</Values><ParamType>String</ParamType><SelectedValue /></Parameters><KeyField>Code</KeyField><IsScalar>false</IsScalar><Hidden>false</Hidden><ShowAdd>true</ShowAdd><ShowEdit>true</ShowEdit><ShowDelete>true</ShowDelete></Settings><ShowSettings>true</ShowSettings></ManagedExtension>"
            };
            eqDb.Store(eds);
            eds = new EqDataStore() {
                ExId = "be_WIDGET_ZONE",
                ExType = (int)ExtensionType.Widget,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<widgets>" +
  "<widget id=\"1ef27b78-0218-420c-bfda-038163510def\" title=\"About the author\" showTitle=\"True\">TextBox</widget>" +
  "<widget id=\"42fcbe7c-2c9c-440a-bad9-65a94472fccc\" title=\"Administration\" showTitle=\"True\">Administration</widget>" +
  "<widget id=\"fb38ecdd-5813-4d10-8c5f-8d901c7c731e\" title=\"Search\" showTitle=\"True\">Search</widget>" +
  "<widget id=\"76dfb6eb-1931-47b6-bece-167a7d0a0b79\" title=\"Tag cloud\" showTitle=\"True\">Tag cloud</widget>" +
  "<widget id=\"ab2b8c27-02c4-46ab-a78f-28939c2a1a09\" title=\"Month List\" showTitle=\"True\">Month List</widget>" +
  "<widget id=\"dc246169-0320-459f-a181-0f7f423dfbb5\" title=\"Page List\" showTitle=\"True\">Page List</widget>" +
  "<widget id=\"f3f65f28-8110-4eff-89ea-5428bc86c823\" title=\"Blogroll\" showTitle=\"True\">Blogroll</widget>" +
"</widgets>"
            };
            eqDb.Store(eds);
            eds = new EqDataStore() {
                ExId = "1ef27b78-0218-420c-bfda-038163510def",
                ExType = (int)ExtensionType.Widget,
                XmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<SerializableStringDictionary>"+
  "<SerializableStringDictionary>"+
    "<DictionaryEntry Key=\"content\" Value=\"Something about the author&#xD;&#xA;\" />"+
  "</SerializableStringDictionary>"+
"</SerializableStringDictionary>"
            };
            eqDb.Store(eds);

            
            eqDb.Close();
        }
    }
}
